package com.fanrui.code.util.core;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;

public class PropertiesUtil{
	private final static String propertiesName = "mysql.properties";
	private final static String configName = "config.properties";
	private final static String logName = "log.properties";
	private final static String configProperties = "config.properties";
	private final static String BuYuHallProperties = "BuYuHall.properties";
	private final static String BuYuGameProperties = "BuYuGame.properties";
	private final static String GateServerProperties = "GateServer.properties";
	public static Properties pro = new Properties();
	public static void readConfig(){
		try{
			String dir =getConfigDir()+configName;
			FileInputStream in = new FileInputStream(dir);
			pro.load(in);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static Reader getResourceAsReader(String resourceName) {
		final String filePath = getConfigDir()+resourceName;
		try {
			FileReader reader = new FileReader(filePath);
			return reader;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static Properties getProperties(String propertiesAddress){
		Properties properties = new Properties();
		try{
			FileInputStream in = new FileInputStream(propertiesAddress);
			properties.load(in);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return properties;
	}
	
	public static Properties getPropertiesAddDir(String fileName){
		return getProperties(getConfigDir()+fileName);
	}
	
	
	public static String getLogDir(){
		return getConfigDir()+logName;
	}
	public static String getDBProperties(){
		return getConfigDir()+propertiesName;
	}
	public static String getConfigProperties(){
		return getConfigDir()+configProperties;
	}
	
	public static String getSystemDir(){
		return System.getProperty("user.dir");
	}
	
	public static String getSystemSeparator(){
		return System.getProperty("file.separator");
	}
	
	
	public static String getBuYuHallProperties(){
		return getConfigDir()+BuYuHallProperties;
	}
	
	public static String getBuYuGameProperties(){
		return getConfigDir()+BuYuGameProperties;
	}
	
	public static String getGateServerProperties(){
		return getConfigDir()+GateServerProperties;
	}
	
	public static String getConfigDir(){
		return getSystemDir()+getSystemSeparator()+"config"+getSystemSeparator();
	}
	
	public static <T> Object getObjectParameter(Properties properties, String parameterName, Class<T> clazz) {
		if (properties != null) {
			String parameterValue = properties.getProperty(parameterName);
			if (parameterValue != null && StringUtils.isNoneBlank(parameterName)) {
				if(clazz == Integer.class) {
					return Integer.parseInt(parameterValue);
				}
				if(clazz == String.class) {
					return parameterValue;
				}
				if(clazz == Boolean.class) {
					return new Boolean(parameterValue);
				}
			}
		}
		return null;
	}
}

